package com.jx.flowershopmanager;

import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.google.gson.Gson;
import com.jx.flowershopmanager.entity.FlowerTypeEntity;
import com.jx.flowershopmanager.entity.User;
import com.jx.flowershopmanager.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import db.MyDatabaseHelper;

public class App extends Application {

    private static Context context;
//    private static MyDatabaseHelper databaseHelper;

    private User.UserData user;
    private static MyDatabaseHelper databaseHelper;
    @Override
    public void onCreate() {
        //获取Contexthb
        super.onCreate();
        context = getApplicationContext();

        /// 创建数据库
        databaseHelper = new MyDatabaseHelper(this, "FlowerDb.db",null,2);
        databaseHelper.getWritableDatabase();

        SPUtils spUtils = new SPUtils(context,"init");
        if (spUtils.getInt("count")>0){
            Log.i("APP","不是第一次打开");
        }else {
            initData();
            initData2();
            spUtils.putInt("count",1);
        }

    }

    /// 初始化鲜花类别
    private void  initData(){
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        /// 读取json文件
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            is = context.getAssets().open("category.json");
            bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[4 * 1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            final String json = bos.toString();
            JSONArray array = new JSONArray(json);
            JSONObject object;

            for (int i =0;i<=array.length()-1;i++){
                object = array.getJSONObject(i);
                String name = object.getString("name");
                ContentValues values = new ContentValues();
                values.put("name",name);
                db.insert("Category",null,values);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                Log.e("App", "getStates", e);
            }
        }
    }
    /// 初始化鲜花数据
    private void  initData2(){
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        /// 读取json文件
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            is = context.getAssets().open("flowers.json");
            bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[4 * 1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            final String json = bos.toString();
            JSONArray array = new JSONArray(json);
            JSONObject object;

            for (int i =0;i<=array.length()-1;i++){
                object = array.getJSONObject(i);
                ContentValues values = new ContentValues();
                values.put("name",object.getString("name"));
                values.put("category",object.getString("category"));
                values.put("origin",object.getString("origin"));
                values.put("describe",object.getString("describe"));
                values.put("url",object.getString("url"));
                values.put("createAt",object.getString("createAt"));
                values.put("price",object.getString("price"));
                values.put("stock",object.getString("stock"));
                db.insert("Flowers",null,values);

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                Log.e("App", "getStates", e);
            }
        }
    }
    public static MyDatabaseHelper getDbHelp(){
        return  databaseHelper;
    }
    //返回
    public static Context getContextObject() {
        return context;
    }

    //    返回用户信息
    public static User.UserData getAppUser(){
        SPUtils spUtils = new SPUtils(context,"user");
        User.UserData user = new User.UserData();
        user.setId(spUtils.getString("id"));
        user.setName(spUtils.getString("name"));
        user.setToken(spUtils.getString("token"));
        return  user;
    }
}
